
package vcp.controllers;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

import db.parking.beans.QuarterReports;
import vcp.application.UserManagement;
import vcp.common.IController;
import vcp.gui.components.VCPScrollTable;
import vcp.models.ReportsModel;
import vcp.views.ReportsView;

/**
 * Reports Controler
 * @author G15
 *
 */
public class ReportsControler implements ActionListener, ChangeListener,ListSelectionListener,
		IController {

	private ReportsModel reportsModel;
	private ReportsView reportsView;

	@Override
	public void addModel(Object model) {
		reportsModel = (ReportsModel) model;
	}

	@Override
	public void addView(Object view) {
		reportsView = (ReportsView) view;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		switch (e.getActionCommand()) {
		case "CreateReport": {
            
			QuarterReports reports = new QuarterReports();
			int lotNum = UserManagement.getInstance().getEmployee().getLotNo();
			int quarter = (int) reportsView.getPnlReportManagement()
					.getComboRivon().getSelectedItem();
			int year = (int) reportsView.getPnlReportManagement()
					.getComboYear().getSelectedItem();
			String reportName = reportsView.getPnlReportManagement()
					.getComboReportType().getSelectedItem().toString();

			reports.setLotNo(lotNum);
			reports.setQuarter(quarter);
			reports.setYear(year);
			reports.setReportName(reportName);
			reportsModel.createQuarterReport(reports);

			break;
		}
		case "ExportGetLotByLotNum": {
			int lotNum = reportsView.getPnlLotsStats().getComboLotNum()
					.getSelectedIndex();
			// System.out.println(level);
			reportsModel.getLotByLotNum(lotNum);

			break;
		}
		case "ManagerGetReport": {
			QuarterReports reports = new QuarterReports();
			int lotNum = (int) reportsView.getPnlQuarterReports()
					.getComboLots().getSelectedItem();
			int quarter = (int) reportsView.getPnlQuarterReports()
					.getComboRivon().getSelectedItem();
			int year = (int) reportsView.getPnlQuarterReports().getComboYear()
					.getYear();
			String reportName = reportsView.getPnlQuarterReports()
					.getComboReportType().getSelectedItem().toString();

			reports.setLotNo(lotNum);
			reports.setQuarter(quarter);
			reports.setYear(year);
			reports.setReportName(reportName);
			reportsModel.getQuarterReport(reports);

			break;
		}
		case "ManagerGetMonthlyReport": {
			Date selectedDate = reportsView.getPnlMonthlyReports()
					.getDateChooser().getDate();
			if (selectedDate != null) {
				int lotNum = (int) reportsView.getPnlMonthlyReports()
						.getComboLot().getSelectedItem();

				SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
				Calendar c = Calendar.getInstance();
				c.setTime(selectedDate);
				// String timestamp = f.format(c.getTime());
				ArrayList<Object> arr = new ArrayList<Object>();
				arr.add(c.get(Calendar.YEAR));
				arr.add(c.get(Calendar.MONTH)+1);
				arr.add(lotNum);
				reportsModel.getMonthlyReport(arr);
			}
			break;
		}
		}

	}

	@Override
	public void stateChanged(ChangeEvent e) {
		// System.out.println(e);
	}
	
	@Override
	public void valueChanged(ListSelectionEvent e) {
	  if(e.getValueIsAdjusting()==false){
		  ListSelectionModel lsm = (ListSelectionModel)e.getSource();
		  DefaultTableModel dtm = reportsView.getPnlMonthlyReports().getDtm();
		  Object[] gscale = reportsView.getPnlMonthlyReports().getGscale();
		  int [] scale = (int[])gscale[lsm.getMinSelectionIndex()];
	      ChartPanel chartPanel = new ChartPanel(getFrequencyChart(scale));
	      reportsView.getPnlMonthlyReports().getPnlGraphTab().removeAll();
		  reportsView.getPnlMonthlyReports().getPnlGraphTab().add(chartPanel);
		  reportsView.getPnlMonthlyReports().getPnlGraphTab().revalidate();
		  reportsView.getPnlMonthlyReports().getPnlGraphTab().getParentFrame().pack();
	  }
	}
	
	private JFreeChart getFrequencyChart(int[] scale){
		 DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		  for(int i=0;i<scale.length;i++){
			  dataset.setValue(scale[i], "Frequency", ""+i);
		  }
		  JFreeChart chart = ChartFactory.createBarChart("Estimated Frequency",
		 	"", "Frequency", dataset, PlotOrientation.VERTICAL,
			false, true, false);
	         
	        
			 // get a reference to the plot for further customisation...
	        final CategoryPlot plot = chart.getCategoryPlot();
	        plot.setBackgroundPaint(Color.lightGray);
	        plot.setDomainGridlinePaint(Color.white);
	        plot.setRangeGridlinePaint(Color.white);

	        // set the range axis to display integers only...
	        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
	        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

	        // disable bar outlines...
	        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
	        renderer.setDrawBarOutline(false);
	        
	        // set up gradient paints for series...
	        final GradientPaint gp0 = new GradientPaint(
	            0.0f, 0.0f, Color.blue, 
	            0.0f, 0.0f, Color.lightGray
	        );
	       
	        renderer.setSeriesPaint(0, gp0);
	       
	        
	        final CategoryAxis domainAxis = plot.getDomainAxis();
	        domainAxis.setCategoryLabelPositions(
	            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0)
	        );
	        return chart;
	}
	
}
